//
//  ZJEmotionToolBar.swift
//  Webo
//
//  Created by 曾静 on 16/7/4.
//  Copyright © 2016年 qq. All rights reserved.
//

import UIKit

enum EmotionToolBarType : Int{
    
    case Default = 1001
    
    case Emoji = 1002
    
    case Lxh = 1003
    
    
}
class ZJEmotionToolBar: UIView {

    var emotionToolClourse:((type:EmotionToolBarType)->())?
    
 
    var currentBtn:UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: -  设置视图
    private func setupUI(){
        
        let defaultButton = addToolBarButtons("left", title: "默认",type: .Default)
        
        //设置初始的选中状态
        defaultButton.selected = true
        
        currentBtn = defaultButton
        
        let emojiButton = addToolBarButtons("mid", title: "Emoji",type: .Emoji)
        
        let lxhButton = addToolBarButtons("right", title: "浪小花",type: .Lxh)
        
        //设置约束
        defaultButton.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.bottom.equalTo(self)
            
            make.width.equalTo(emojiButton)
            
        }
        
        emojiButton.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(self)
            
            make.width.equalTo(lxhButton)
            
            make.leading.equalTo(defaultButton.snp_trailing)
            
        }
        
        lxhButton.snp_makeConstraints { (make) -> Void in
            
            make.top.trailing.bottom.equalTo(self)
            
            make.leading.equalTo(emojiButton.snp_trailing)
            
        }
    }
    
    
    //创建改变底部按钮选中状态的方法,共外界调用
    
    func changToolBarSelected(indexPath:NSIndexPath){
//        
//        let tag = indexPath.section + tagMargin
//        
//        print(tag)
        
        let button = self.viewWithTag(indexPath.section + tagMargin)! as! UIButton
        
        if currentBtn == button{
            
            return
        }
        
        //设置按钮的选中状态
        currentBtn?.selected = false
        
        button.selected = true
        
        //可选类型的范围大,可以将对象直接复制给对象的可选类型
        currentBtn = button
        
        
    }
    //创建添加按钮的公共方法
    func addToolBarButtons(imageName:String,title:String,type:EmotionToolBarType)->UIButton{
        
        let button = UIButton()
        
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imageName)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imageName)_selected"), forState: UIControlState.Selected)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        button.setTitle(title, forState: UIControlState.Normal)
        
        button.addTarget(self, action: "addToolBarClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)
     
        button.tag = type.rawValue
        //注意添加按钮的方式与UIStackView的区别
        addSubview(button)
        
        
        return button
        
        
        
    }
    
    func addToolBarClick(button:UIButton){
        
        if currentBtn == button{
   
            return
        }
        
        //设置按钮的选中状态
        currentBtn?.selected = false
        
        button.selected = true
        
        //可选类型的范围大,可以将对象直接复制给对象的可选类型
        currentBtn = button
        
        
        
        emotionToolClourse?(type: EmotionToolBarType(rawValue: button.tag)!)
        
        
    }

}
